﻿using System;
using System.Windows;
using System.Windows.Controls;
using appInterfaces;
using appDataContracts;
using System.Collections.Generic;

namespace appAdmin
{
    /// <summary>
    /// Logique d'interaction pour AppWindow.xaml
    /// </summary>
    public partial class AppWindow : Window
    {
        private appAdmin.myAdminService.AdminClient adminService = null;

        public AppWindow(SerializedAdmin admin, appAdmin.myAdminService.AdminClient adminService)
        {
            this.adminService = adminService;
            InitializeComponent();
            update();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void dgSupplier_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (dgSupplier.SelectedItems.Count > 0)
            {
                btEdit_Click(sender, e);
            }
        }

        private void dgSupplier_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgSupplier.SelectedItems.Count > 0)
            {
                btEdit.Visibility = Visibility.Visible;
                btDelete.Visibility = Visibility.Visible;
            }
            else
            {
                btEdit.Visibility = Visibility.Hidden;
                btDelete.Visibility = Visibility.Hidden;
            }
        }

        private void btEdit_Click(object sender, RoutedEventArgs e)
        {
            new showSuppliers(adminService, this, (SerializedSupplier)dgSupplier.SelectedItem).ShowDialog();
        }

        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            adminService.deleteSupplier(((SerializedSupplier)dgSupplier.SelectedItem).SUPPLIERID);
            update();
        }

        private void btAddProduct_Click(object sender, RoutedEventArgs e)
        {
            new showSuppliers(adminService, this, null).ShowDialog();
        }

        public void update()
        {
            dgSupplier.ItemsSource = (List<SerializedSupplier>)adminService.getSuppliers();
        }
    }
}
